package com.paipai.demo1;

import java.security.PrivateKey;
import java.security.PublicKey;

import org.junit.Before;
import org.junit.Test;

import com.paipai.utils.JwtUtils;
import com.paipai.utils.RsaUtils;
import com.paipai.utils.UserInfo;

public class test1 {
	
	 private static final String pubKeyPath = "D:\\javaplug\\rsa\\rsa.pub";

	    private static final String priKeyPath = "D:\\javaplug\\rsa\\rsa.pri";

	    private PublicKey publicKey;

	    private PrivateKey privateKey;

	    @Test
	    public void testRsa() throws Exception {
	        RsaUtils.generateKey(pubKeyPath, priKeyPath, "23456");
	    }

	    @Before
	    public void testGetRsa() throws Exception {
	        this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
	        this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
	    }

	    @Test
	    public void testGenerateToken() throws Exception {
	        // 生成token
	        String token = JwtUtils.generateToken(new UserInfo(12L, "aa"), privateKey, 1);
	        System.out.println("token = " + token);
	    }

	    @Test
	    public void testParseToken() throws Exception {
	        String token = "eyJhbGciOiJSUzI1NiJ9.eyJpZCI6MTIsInVzZXJuYW1lIjoiYWEiLCJleHAiOjE1NzgxMTkxNDF9.O6ddTuukYyHGe8PMAPPJmD-T1OfUTSsP0GcB9MbTLhFQFaOaA35wDhhWkJkbgcmXxe3G_KJx8BLwdRbSTkELGj0W0nNRz5bOox4Jfl66iDDexMq5Gz3iV3Fu4eXyz9AmvvY1dUIF8_FXkcQDlm99WF7ig62zm2XAKulCrd-FXjo";

	        // 解析token
	        UserInfo user = JwtUtils.getInfoFromToken(token, publicKey);
	        System.out.println("id: " + user.getId());
	        System.out.println("userName: " + user.getUsername());
	    }
	
	

}
